package com.paipai.helper.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.paipai.helper.R;
import com.paipai.helper.service.UIHelperService;
import com.paipai.helper.util.CmdUtil;
import com.paipai.helper.util.SPConstant;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText mSeedCountET;
    private EditText mCountDownET;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // create default notification channel
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            final String channelId = getString(R.string.default_floatingview_channel_id);
//            final String channelName = getString(R.string.default_floatingview_channel_name);
//            final NotificationChannel defaultChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_MIN);
//            final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//            if (manager != null) {
//                manager.createNotificationChannel(defaultChannel);
//            }
//        }
        Intent intent = new Intent(MainActivity.this, UIHelperService.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startService(intent);
        init();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_save:
                boolean isSuccess = saveConfigData();
                Toast.makeText(this, "保存数据成功"+isSuccess, Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }

    private void init() {
        findViewById(R.id.btn_save).setOnClickListener(this);
        mSeedCountET = (EditText)findViewById(R.id.et_seed_count);
        mCountDownET = (EditText)findViewById(R.id.et_seed_count_down);
        CmdUtil.getRoot();
    }

    private boolean saveConfigData(){
        String count = mSeedCountET.getText().toString();
        if(TextUtils.isEmpty(count)){
            count = "8";
        }
        int seedCount = Integer.valueOf(count);
//        String timeStr = mCountDownET.getText().toString();
//        if(timeStr.indexOf(":") == -1){
//            Toast.makeText(this, "时间格式不正确", Toast.LENGTH_SHORT).show();
//            return false;
//        }
//        String times[] = timeStr.split(":");
//        long time =Integer.valueOf(times[0])*1000*60*60+Integer.valueOf(times[1])*1000*60+Integer.valueOf(times[2])*1000;
//        final SimpleDateFormat sp = new SimpleDateFormat(" HH:mm:ss");
//        Date date = new Date(time);
//        Log.d("FloationView", "倒计时时间"+ sp.format(date).toString());
        Map<String, Object> map = new HashMap<>();
        map.put(SPConstant.SP_SEED_COUNT, seedCount);
//        map.put(SPConstant.SP_COUNT_DOWN, time);
        SPConstant.saveDataToCache(this, map);
        return  true;
    }
}
